I have this schedule tasks using apscheduler.schedulers.asyncio.AsyncIOScheduler when I say -startc the schedule starts. When I say stopc I want the schedule to stop.
#commands.command()
#commands.has_permissions(administrator=True)
async def startc(self, ctx):
channel = ctx.channel.id
self.client.scheduler.add_job(self.cardDrop, CronTrigger(second='0, 10, 20, 30, 40, 50'), args=[channel])
#commands.command()
#commands.has_permissions(administrator=True)
async def stopc(self, ctx):
Use stop()
#commands.command()
#commands.has_permissions(administrator=True)
async def startc(self, ctx):
channel = ctx.channel.id
self.client.scheduler.add_job(self.cardDrop, CronTrigger(second='0, 10, 20, 30, 40, 50'), args=[channel])
#commands.command()
#commands.has_permissions(administrator=True)
async def stopc(self, ctx):
self.client.scheduler.stop()
I found the answer thank you for your help.
it was
self.client.scheduler.shutdown(wait=False)
False is if you want to stop the task immediately. True is when you want to wait till the task is complete and then stop
Related
I wanted to create a clear command where if example .clear #user#0000 100 it will clear the last 100 messages #user#0000 sent. Here is my code:
#commands.command()
#commands.has_permissions(manage_messages=True)
async def clear(self, ctx, amount=1):
await ctx.channel.purge(limit=amount + 1)
#clear.error
async def clear_error(self, ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send('Sorry, you are missing the ``MANAGE_MESSAGES`` permission to use this command.')
This code i provided only deletes depeneding how many messages will be deleted in a channel. I want to make a code where the bot will delete messages from a specific user. If this is possible, please let me know. I will use the code as future reference. Much appreciated.
You can use check kwarg to purge messages from specific user:
from typing import Optional
#commands.command()
#commands.has_permissions(manage_messages=True)
async def clear(self, ctx, member: Optional[discord.Member], amount: int = 1):
def _check(message):
if member is None:
return True
else:
if message.author != member:
return False
_check.count += 1
return _check.count <= amount
_check.count = 0
await ctx.channel.purge(limit=amount + 1 if member is None else 1000, check=_check)
I have been trying to code a Discord bot for my own server.
However, it seems like, whenever I add more commands to the code, the ban, and kick functions no longer work correctly.
I've tried rewriting the code multiple times, but it did not work.
I've tried rearranging the codes, and that did not work as well.
client = commands.Bot(command_prefix = '!')
#client.command()
async def kick(ctx, member : discord.Member, *, reason = None):
await member.kick(reason = reason)
#client.command()
async def ban(ctx, member : discord.Member, *, reason = None):
await member.ban(reason = reason)
curseWord = ['die', 'kys', 'are you dumb', 'stfu', 'fuck you', 'nobody cares', 'do i care', 'bro shut up']
def get_quote():
response = requests.get("https://zenquotes.io/api/random")
json_data = json.loads(response.text)
quote = json_data[0]['q'] + " -" + json_data[0]['a']
return(quote)
#ready
#client.event
async def on_ready():
print('LOGGED IN as mr MF {0.user}'.format(client))
#client.event
#detect self messages
async def on_message(message):
if message.author == client.user:
return
#greeting
if message.content.startswith('hello'):
await message.channel.send('ay whatup ma gamer')
#help
if message.content.startswith('!therapy'):
quote = get_quote()
await message.channel.send(quote)
#toxicity begone
msg_content = message.content.lower()
if any(word in msg_content for word in curseWord):
await message.delete()
#environment token
token = os.environ['token']
client.run(token)
You have overwritten the on_message event, which by default processes commands (those marked with the #client.command() decorator). You need to explicitly tell the library to process commands when you overwrite it. As noted in the discord.py documentation, you should add this line at the end of the on_message event:
await client.process_commands(message)
Currently I am working on a discord bot for a Pokemon and I will be running, I have the encounter system down, but have been having trouble with the damage system. Currently this is what the whole code looks like.
import discord
import os
import random
import asyncio
client = discord.Client()
from discord.ext import commands
client = commands.Bot(command_prefix='!')
#client.event
async def on_ready():
print('aaaaand were scuffed'.format(client))
###Biome encounter list###
DesertList2 = ['charmander', 'charmeleon', 'charizard']
###commands###
#desert enc
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!desert'):
mon = random.choice(DesertList2)
print(mon)
await message.channel.send(mon)
#client.event
async def damage(ctx, num1, num2, num3):
result = (num1+num2)/num3
await ctx.send(result)
await client.process_commands(message)
Note(s): I have client.run, just not shown for obvious reasons.
You'd have to create separate argument variables.
#client.command()
async def damage(ctx, num1 : int, num2 : int, num3 : int): # for your 1 2 4 example, 3 number arguments would be sufficient
result = (num1+num2)/num3
await ctx.send(result)
This would do (1+2)/4 and should give you 0.75.
Also, after looking at your imgur image, this is what your on_message function needs to look like:
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!desert'):
mon = random.choice(DesertList2)
print(mon)
await message.channel.send(mon)
await client.process_commands(message)
#2P1XLS I took the liberty of making your desert command an actual command, since it was previously in the on_message event, and it seems to me that you're trying to use it as a command. However, you can still run it how you were running it previously: !desert. I've also added the await client.process_commands(message) in the on_message event, since that's where it belongs. Try this out:
import discord
import os
import random
import asyncio
client = discord.Client()
from discord.ext import commands
client = commands.Bot(command_prefix='!')
#client.event
async def on_ready():
print('aaaaand were scuffed'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
await client.process_commands(message)
###Biome encounter list###
DesertList2 = ['charmander', 'charmeleon', 'charizard']
###commands###
#desert command
#client.command()
async def desert(ctx):
mon = random.choice(DesertList2)
print(mon)
await ctx.send(mon)
#damage command
#client.command()
async def damage(ctx, num1 : int, num2 : int, num3 : int):
result = (num1 + num2) / num3
await ctx.send(result)
If you run !damage 1 2 4, then it would do (1+2)/4 and would return 0.75. I've tried this code out myself, so it should definitely work. Make sure to copy it exactly. Hope this helped you!
Here is a clean and simple effective way of a calculator command, you can give it endless arguments for it to calculate
By using *, you can pass multiple numbers and .join will join each of the arguments you pass. Here's a simple example,
#client.command()
async def calculate(ctx, operation, *nums):
if operation not in ['+', '-', '*', '/']:
await ctx.send('Please use a valid operation type')
return
var = f' {operation} '.join(nums)
await ctx.send(f'{var} = {eval(var)}')
Alright so guys i need some help. Basically i am making an discord bot. I'm having problems with clear(purge) command. So this is my code so far:
#client.command(aliases= ['purge','delete'])
#commands.has_permissions(manage_messages=True)
async def clear(ctx, amount : int):
if amount == None:
await ctx.channel.purge(limit=1000000)
else:
await ctx.channel.purge(limit=amount)
my problem here is this if amount == None .
Please help me!
Im getting an error that i have missing requied argument...
That's because you're not giving amount the default value None. This is how you should define the function:
#client.command(aliases= ['purge','delete'])
#commands.has_permissions(manage_messages=True)
async def clear(ctx, amount=None): # Set default value as None
if amount == None:
await ctx.channel.purge(limit=1000000)
else:
try:
int(amount)
except: # Error handler
await ctx.send('Please enter a valid integer as amount.')
else:
await ctx.channel.purge(limit=amount)
This one is working for me
#client.command()
#has_permissions(manage_messages = True)
async def clear(ctx , amount=5):
await ctx.channel.purge(limit=amount + 1)
I got it from a youtuber channel, you can put the number of the messages u want to clear after the clear command, EX:
-clear 10
Try this:
#client.command(aliases = ['purge', 'delete'])
#commands.has_permissions(manage_messages = True)
async def clear(ctx, amount: int = 1000000):
await ctx.channel.purge(limit = amount)
This should work, as I have a clear command that looks almost exactly like this. The amount param is being converted to an int and if the client doesn't give parameters, the amount will be set at 1000000.
This is the one I'm using atm.
#bot.command(name='clear', help='this command will clear msgs')
async def clear(ctx, amount = 50): # Change amount
if ctx.message.author.id == (YOUR USER ID HERE):
await ctx.channel.purge(limit=amount)
else:
await ctx.channel.send(f"{ctx.author.mention} you are not allowed")
I've just added an 'if' statement for being the only one who can use that command. You can also allow an specific role.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='>')
#client.event
async def on_ready():
print("Log : "+str(client.user))
#client.command()
#commands.has_permissions(manage_messages=True)
async def clear(ctx, amount: int):
await ctx.channel.purge(limit=amount+1)
await ctx.message.delete()
client.run("token")
you can do this:
#client.command(aliases= ['purge','delete'])
#commands.has_permissions(manage_messages=True)
async def clear(ctx, amount :int = -1):
if amount == -1:
await ctx.channel.purge(limit=1000000)
else:
await ctx.channel.purge(limit=amount)
I think it should work, if the client doesn't give parameters, the amount will be set at "-1"
An error occurs using this code it says bot is not defined. I don't understand cogs that much but I understand a bit of classes too. I wanted to know how functions work in a cog and how variables are assigned like guildstats = ... below.
This is my code: (I am trying to make a database using the discord bot in a guild. The code works without using cogs but I wanted it to be easier to debug any errors so I went for cogs.)
class Boot(commands.Cog):
def __init__(self, bot):
self.bot = bot
guildstats = pd.read_excel('DiscordStats.xlsx',sheet_name=0)
userstats = pd.read_excel('DiscordStats.xlsx',sheet_name=1)
def dataframe_to_excel(df1 = guildstats, df2 = userstats):
with pd.ExcelWriter('DiscordStats.xlsx', mode = 'w') as writer:
df1.to_excel(writer, index=False, sheet_name = 'GuildStats')
df2.to_excel(writer, index=False, sheet_name = 'UserStats')
def guildstats_writer():
guild_row_data = []
for guild in self.bot.guilds:
if '\''+str(guild.id) not in guildstats['GuildID'].to_list():
guild_row_data.append([guild.name,'\''+str(guild.id),'','',False])
else:
pass
guild_row = pd.DataFrame(guild_row_data,columns = guildstats.columns.to_list())
guildstats1 = guildstats.append(guild_row,ignore_index=True)
Boot.dataframe_to_excel(df1=guildstats1)
#commands.Cog.listener()
async def on_ready(self):
Boot.guildstats_writer(self)
await sbot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="for MeepMop ~help"))
print(f'{bot.user} is connected to the following guild:')
for guild in bot.guilds:
print(f'{guild.name} (id: {guild.id})')
def setup(bot):
bot.add_cog(Boot(bot))
You're trying to call the variable bot when you never defined it, your program has no idea what the variable "bot" is, try changing all the times you called bot to instead call self.bot instead
For example your on_ready function should look like this:
#commands.Cog.listener()
async def on_ready(self):
Boot.guildstats_writer(self)
await self.bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="for MeepMop ~help"))
print(f'{self.bot.user} is connected to the following guild:')
for guild in self.bot.guilds:
print(f'{guild.name} (id: {guild.id})')