Hi so I have a command for my bot -set_event (event code). If that occurs on one server the change follows to another. I'm curious if there is a way to stop this from happening. Thanks.
#commands.command()
async def set_event(self, ctx, arg):
global event_code
event_code = arg.upper()
Related
I want to make a word chaining bot on discord, and I want it to save all the messages in the channel so I can check for duplicates, is this possible?
Of course this is possible. And it is extremely easy to do.
import discord
client = discord.Client()
log = open("log.txt", "a")
def add_message(msg):
log.write(msg)
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
add_message(message) # You can check the channel with message.channel.id
client.run('your token here')
This should work
Edit: If your concern is to check every message against every new message, as other said, switch to a database.
MySql is very easy to setup (compared to PostgreSQL) and connect to Python, and you can check for duplicates very easily.
I saw many tutorials online to check my code, but none of them seems to help my problem. Here is my code for joining a voice channel:
#commands.command()
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
I'm not making anything too fancy right now, I just want a way to get this to work. Thanks.
Here's my code for joining a vc
channel = ctx.message.author.voice.channel
if ctx.voice_client is not None:
return await ctx.voice_client.move_to(channel)
await channel.connect()
If the problem still persists, I need you to send more part of the code (Initialisation and Execution)
I'm using Discord.py how can I make the bot execute some operations when a user post a new message?
I highly recommend reading through the docs first before coming here to ask a question. However, you can use a client event and on_message to check every new message, for example:
#client.event
async def on_message(message):
if message.content == "Hi":
await message.channel.send("Hello!")
I want to start and stop a start when the user want, but I get an error RuntimeError: Task is already launched and is not completed. , how can I stop a task if it is active and then restart it?
#tasks.loop(minutes=1)
async def FunctionTask():
print("Task running")
#client.event
async def on_ready():
FunctionTask.start()
#client.command()
async def start(ctx):
FunctionTask.restart()
#client.command()
async def stop(ctx):
FunctionTask.stop()
Thank you for any answer.
I am not getting the issue you're having but you should try the solution in the docs
Note: If the internal function raises an error that can be handled before finishing then it will retry until it succeeds.
If this is undesirable, either remove the error handling before stopping via clear_exception_types() or use cancel() instead.
Try using cancel() instead of stop() and see if that fixes it.
So my bot DM's users and asks them questions, but I need to be able to see their response in a channel.
So far I have this:
#bot.event
async def on_message(message):
channel = bot.get_channel(642759168247463937)
await channel.send('message')
but it begins when any message is sent in any channel and it also responds to itself, causing an endless loop of spam.
I'm new to Discord.py so I honestly don't have a clue on how to resolve this issue.
My approach would be to check if the message came from DMs:
#bot.event
async def on_message(message):
if not message.guild:
return
channel = bot.get_channel(642759168247463937)
await channel.send('message')
This works because message.guild is None if the message was sent in DMs. This prevents the spam problem, since the message is not forwarded to the bot's DMs.
However, to avoid the spam in general, it is good practice to avoid responding to bots altogether, unless necessary. Luckily, User and Member objects have an attribute called bot that helps us here:
if message.author.bot:
return
Note that this is done automatically for commands using the ext.commands framework.