I'm creating a simple Twitch bot for personal use. I'm using twitchio.ext commands. Everything works fine, I'm connected, I'm able to print all the messages from the chat, my bot responds to commands but I'm not able to send a message to chat and I don't know why.
from twitchio.ext import commands
class Bot(commands.Bot):
def __init__(self):
super().__init__(token='oauth:censored', prefix='g ', nick = "nick of the bot", irc_token = "censored", initial_channels=['channel'])
async def event_ready(self):
print(f'Using bot as {self.nick}')
#commands.command()
async def test(self, ctx: commands.Context):
await ctx.send('test')
print("printed")
bot = Bot()
#bot.event()
async def event_message(msg):
print(msg.author.name)
print(msg.content)
await bot.handle_commands(msg)
bot.run()
When I type "g test" in chat, the message "test" is not sent but the message "printed" is printed to the console. Do you know where could be the problem? Also, I would like to ask if there is any way how to send a message to chat directly without responding to a command or event.
Try adding the bot as a mod
The 'nick' in the bot init() function doesn't seem to do anything the nick is linked to your IRC username.
I'm assuming the channel in your initial_channels in the init() func isn't actually the channel you put there since you said you were able to type in the chat.
If the channel that you are using the bot from(linked to the oauth key) is not the same channel as you are connecting to in the chat then you probably need to add the bots username as a moderator in that chat
try
/mod [bot_username]
in the chat as the channel owner to do this
Also if your bot is the same account you are sending commands from on twitch the rate limit will be reached unless they are a mod. So either make a dedicated account for your bot or mod the bot on the server
Related
So I want to know if it is possible, that a bot gets the content sent to it in a dm and send that in a specifyed channel on a server.
So basically you dm the bot the word "test" and the bots sends the word in a channel of a server
Yes, it is possible for a bot to receive a direct message and then repost the message in a specified channel on a server. This can be done using the Discord API.
You can do the following:
Create a Discord bot and add it to your server. You can do this using the Discord developer portal.
Use the Discord API to listen for messages sent to the bot in a DM. You can do this using the message event and the DMChannel class in the Discord API.
When the bot receives a DM, use the Discord API to repost the message in the specified channel on the server. You can do this using the send method of the TextChannel class in the Discord API.
Yes, this is possible:
First, we use the on_message() event to detect when a message is sent.
We then check if the message is sent in a DM.
If so, we will send a message to a specific channel.
Here is one way you can implement it:
# import ...
bot = discord.Bot(...)
#bot.event
async def on_message(message):
# check if it's
if isinstance(message.channel, discord.DMChannel):
# get the channel from ID
channel = client.get_channel(CHANNEL_ID) # put channel ID here
await channel.send("test") # whatever you want to send
#bot.event:
async on_message(message):
if message.DMChannel:
# define channel and send message to channel.
You can also refer to DOCS.
I have for the moment a code that sends a message but only for my own discord server as a bot.
What I would like it's automated sending message to others servers to simulate an activity as if it were my main account
def sendMessage(ch, ms):
client = discord.Client()
#client.event
async def on_ready():
channel = client.get_channel(ch)
await channel.send(ms)
return ""
client.run('token')
How to make the sendMessage code sending a message using my main account?
Please, do not do this.
Like Silvio said, if you find a way to send messages as a user through code and you are caught then you might get banned. Don't say we didn't warn you!
I want to make that when a user join to the server where this bot is located, the bot should send him a greeting message in the dm. But when connecting, the bot does not send a message. No errors are displayed. How can I fix this? My code:
#client.event
async def on_member_join():
await member.send("Welcome!")
#client.eevent
async def on_member_join(member):
await member.send("Welcome!")
you need to have an arguement in the function!
Also you need to enable intents in your bot
client = discord.Client(command_prefix="!", intents=discord.Intents.all(),case_insensitive=True)
you also need to enable all intents in the discord Developer portal
I wanted to make a bot in Python which can automatically set up Servers for you. I know how to do everything with the bot except the part, where the Bot automatically adds you.
How can I authorize the application to allow it to add me to servers and how do I make it add me to a server afterwards?
Edit: I tried https://github.com/discord/discord-oauth2-example but that didn't work because of an invalid redirect URI.
I don't know about that, but I have a simple script that creates a guild, then creates an invite link and invites you to the guild that's been created.
All you have to do afterwards is "!create Guild_Name"
Here's the code;
#bot.command()
async def create(ctx, *, name):
guild = await bot.create_guild(name=name)
channel = await guild.create_text_channel('welcome')
invite_link = await channel.create_invite()
await ctx.send(f"Succesfully created **{guild.name}**. (ID: {guild.id})\n"
f"{invite_link}")
I've just started writing Discord bots. I'm currently working on a bot that helps with Dungeons & Dragons games (dice rolling and initiative tracking are working at the moment). I've gotten my bot to send private rolls to a DM with the user that called the command within the server, but within the actual DM, the bot only responds to the help command.
I've read through this explanation, but it still doesn't seem useful to me, since I want it to respond to commands in DMs rather than grab the content of the messages sent to it in DMs. For example, let's say I'm in a server with the bot, and I use the whisperRoll command, which causes the bot to send me a DM with my dice roll result. Now that I have a private chat with the bot, I'd like to make another roll just for myself so that other players can't see it, so I try to use the roll command in the DM channel. What I'd like it to do is respond to the command in the DM in the same way it would respond in a server.
I'm wondering if there's possibly a set of default commands that are registered as "valid" for the bot to respond to in DMs that I'm missing? I can't really find an answer to this anywhere, and I don't even know where to start.
Thanks so much for the help!
EDIT: My current code, which is giving me a CommandInvokeError:
def _prefix_callable(bot, msg):
prefix = '!'
guild = msg.guild
if p.prefixInfo == []:
pass
else:
prefix = p.getPrefix(guild)
if not guild:
return commands.when_mentioned_or(prefix)(bot, msg)
return commands.when_mentioned_or(prefix)(bot, msg)
bot = commands.Bot(command_prefix=_prefix_callable, description=description, help_command = None)
p.getPrefix(guild) calls this code:
def getPrefix(self, guild):
for data in self.prefixInfo:
if data[0] == str(hash(guild)):
return data[1]
return "!"
I'm currently having it search through a csv to find the right prefix for the given guild.
You can use the discord.channel.DMChannel object
For example:
async def check(ctx, arg):
if isinstance(ctx.channel, discord.channel.DMChannel):
await ctx.send(arg)
For usage in dms with prefix
def get_prefix(message):
prefix = "?" # default prefix
if not message.guild: # if in dms
return commands.when_mentioned_or(prefix)(bot, message)
#you can set a guild/server specific one here
return commands.when_mentioned_or(prefix)(bot, message)
bot = commands.Bot(command_prefix=get_prefix, case_insensitive=True) #bot allows you to make commands
Then for the command, you would need to do:
#bot.command()
async def whisperroll(ctx):
await ctx.author.send("some random thing")