How To Make Discord.py dev only Commands - discord.py

How To Make Discord.py dev only Commands
def restart_bot():
os.execv(sys.executable, ['python'] + sys.argv)
#Bot.command(name= 'restart')
async def restart(ctx):
await ctx.send("Restarting bot...")
restart_bot()

You can do with #commands.is_owner()
basically like this:
#Bot.command(name = 'restart')
#commands.is_owner()
async def restart(ctx):
await ctx.send("Restarting bot...")
restart_bot()
thank me later :D

Related

Counting command (discord.py)

Im trying make the bot count to whatever the user says but it is not sending anything
#commands.cooldown(1, 5, commands.BucketType.user)
#client.command()
async def count(ctx, num):
for i in range(1, num+1):
await ctx.channel.send(i)
There is no error.
Can anyone help
The problem is that num has to be an integer. Here's how you should do it :-
#client.command()
async def count(ctx, num: int):
for i in range(1, num+1):
await ctx.send(i)

I got stuck in this anti ads link with blacklist role

I starting to learning discord.py and I got some idea to make a anti advertising link function. My plan is detect a link that have http or https in link they send and give they a role and in my plan this role name is gg and this is my code
#Advertising link detection
#client.event
async def on_message(message):
if 'https' in message.content.lower():
await message.delete()
await message.channel.send(f"{message.author.mention} This link not ALLOWED")
else:
await client.process_commands(message)
#client.event
async def on_message(message):
if 'http' in message.content.lower():
await message.delete()
await message.channel.send(f"{message.author.mention} This link not ALLOWED")
else:
await client.process_commands(message)
#autorole
autorole = discord.utils.get(ctx.guild.roles, name='gg')
await ctx.add.roles(autorole)
I really no idea how to do this function
So in your code you have two functions with the same name, so the second one overrides the first one and only the second one is used and the first one is discarded. Most of the time situations like this can be avoided by using a single event with the code combined. in your case that would work too using a elif or the or keyword. so
#client.event
async def on_message(message):
if 'https' in message.content.lower() or 'http' in message.content.lower():
await message.delete()
await message.channel.send(f"{message.author.mention} This link not ALLOWED")
else:
await client.process_commands(message)
but I would highly suggest using regex to match for urls
You have called same event 2 times and that causes bot to stuck use else if two add 2 if statement
You have 2 events
on_message(message):

How to invoke asyncio code that blocks, and reach the next line?

How can I invoke f and reach the next line?
from SomeLib import f
f()
print('never reaches')
I would prefer not to mess with the internals of 'SomeLib', but f it's a quick fix I'll do it:
def f():
asyncio.get_event_loop.run_until_complete(ag())
async def ag():
async with websockets.client.connect(...) as websocket:
:
await wait_for_recv(...)
async def wait_for_recv(...):
while True:
message = await asyncio.wait_for(websocket.recv(), timeout=time_out)
process(message)
Calling ag directly is an option, but how to do it?
I've tried using a thread.
I've tried executors.
I've tried new_event_loop.
I'm out of ideas.
I figured out a solution. I suspect it is terrible, so I would appreciate feedback.
At the callsite:
worker_thread = threading.Thread(target=worker)
worker_thread.start()
def worker:
f()
And fiddling the library:
def f():
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
self.loop.call_soon_threadsafe(ag)
# added this
def kill(self):
self.loop.stop()

How to use guild.id in tasks

I am trying to make a command that loops to check when a server wipes but I found out that you can't use ctx.guild.id in a tasks is there anyway to make a command loop but still be able to use guild:
#tasks.loop(seconds=120)
async def pop_status():
for x in collection.find():
if x["_id"] == client.get_guild(id):
wipe = x["pop"]
response = requests.get('https://api.battlemetrics.com/servers/' + wipe)
pass_times = response.json()
Server_wipe = pass_times['data']['attributes']['details']['rust_last_wipe']
print(Server_wipe)
There's a few options, you can pass the guild argument in the task when starting it, or you can get it inside the task:
#tasks.loop(seconds=120)
async def pop_status(guild):
# you can use the guild argument
# You have to pass it when starting it, example in a command
#bot.command()
async def start(ctx):
pop_status.start(ctx.guild)
Getting the guild in the loop itself
#tasks.loop(seconds=120)
async def pop_status():
guild = bot.get_guild(ID_HERE)
PS: You shouldn't really use the requests library, it's blocking, you should use aiohttp instead

repeat the song when the command is used again

I want it to say the same file again when using the command again but it doesn't work please help
#client.command()
async def join(ctx):
voicechannel = discord.utils.get(ctx.guild.channels, name='text')
vc = await voicechannel.connect()
vc.play(discord.FFmpegPCMAudio('deneme/audio.mp3'), after=lambda e: print('done', e))

Resources