To use an existing timeout when the bot restarts - discord.py

How do I resume the existing timeout when I restart the bot?
Is there only one way to save it to another database?
I'm curious how you save it

You could use a simple JSON file.
#bot.listen()
async def event()
with open('NameOfFile.json','r') as f:
example = json.load(f)
#code
with open('NameOfFile.json','w') as f:
json.dump(example_count,f,indent=4)

Related

Trying to make a suggestion but channel id gives an error for the command

I've got an issue but I am getting no errors and from the code I just wrote as shown below
#client.commands
async def hello():
channel = int(797915093954199565)
await channel.send('Hey what are you doing?')
I am trying to make a command where the user can talk to the bot, and it responds back with something, its just a starting command but I'm having trouble with these small things, the rest of the bot works but its just this issue i'm having please help!
So assuming the rest of your bot and code works, your "hello" command isn't working because you have
#client.commands #its client.command in this case, as it seems you are not using any cogs, and you must call the client with () these double parentheses
async def hello(): #Here you have not passed ctx
channel = int(793744805615632394) #It seems what you would like to do here is send it to a specific channel, however, in the code below, I have set it so it just sends to the channel it was used in. The correct use is client.get_channel(793744805615632394)
await channel.send('Hey what are you doing?')
Here is the command but fixed:
So assuming the rest of your bot and code works, your "hello" command isn't working because you have
#client.command()
async def hello(ctx):
await ctx.send('Hey what are you doing?')
You should also refer to the py docs to learn more about it: https://discordpy.readthedocs.io/en/latest/

Use asyncio with neovim remote plugins

I want to write a vim plugin that listens to Server-sent events. Since I am most fluent with python3 and use neovim, I figured it would be a good idea to use the neovim remote plugin API.
Obviously, listening for messages from the network must not be blocking, so asyncio must be involved somehow. But I was not able to figure out how to combine the two. Somewhere I would have to run an event loop. However, pynvim already runs its own event loop, so I probably should hook into that.
#pynvim.plugin
class MyPlugin:
def __init__(self, nvim):
self.nvim = nvim
#pynvim.command('Connect', nargs='1')
async def connect(self, args):
url = base_url + args[0]
async with sse_client.EventSource(url) as event_source:
for raw in event_source:
try:
msg = json.loads(raw)
except json.JSONDecodeError:
continue
do_something(msg)
This example doesn't work. The Connect command is not available in neovim (it was before I made it async).
Not sure if this is the best answer, but this is what I have found to work:
asyncio seems to keep a reference to the current loop, so asyncio.ensure_future() can be used to schedule async code. However, that async code will crash if it tries to access vim internals. In order to do that, you need to call yet another callback with nvim.async_call().
#pynvim.plugin
class MyPlugin:
def __init__(self, nvim):
self.nvim = nvim
async def _connect(self, url):
async with sse_client.EventSource(url) as event_source:
for raw in event_source:
try:
msg = json.loads(raw)
except json.JSONDecodeError:
continue
self.nvim.async_call(do_something, msg)
#pynvim.command('Connect', nargs='1')
def connect(self, args):
url = base_url + args[0]
asyncio.ensure_future(self._connect(url))

Discord.py How to get the list of all the audit logs?

I'm searching a way to get all the audit logs printed on a txt file, because I want then the bot to send that txt file to the admin's email. How can I get all the audit logs?
You use async for ... in guild.audit_logs(limit=100)
The function has a lot more different parameters, all of which you can find here.
Here is an example on how to use it:
async def save_audit_logs(guild):
with open(f'audit_logs_{guild.name}', 'w+') as f:
async for entry in guild.audit_logs(limit=100):
f.write('{0.user} did {0.action} to {0.target}'.format(entry))
#client.event
async def on_message(message):
if message.content.startswith('audit'):
await save_audit_logs(message.channel.guild)
Additional info:
This is an async iterator, meaning it can only be used in async functions.
Every audit log entry is of this class, I recommend reading up on it a bit.

I have problems with command that gets what user typed after command

I am doing a Discord Bot for one SandBox page.
I have already made a program that scraps the data from ANY user you want it to. (It's input and after that it scraps the data)
Now there is a new problem. I want to make a command "rr2.info.UserHere"
The rr2. is prefix, but I want to make a command that gets what you typed after rr2.info. and saves it into a variable.
I have tried with some code I found online, but it wasn't working. After that I couldn't find ANYTHING else.
#client.event
async def on_message():
if message.content.startswith('rr2.info.'):
#This is the part I need help with! :D
I need a way of getting ANYTHING typed after rr2.info. and that command done!
It's not a good practice to use the on_message() as commands.
Better use command() fom discord.ext.commands.
Apparently you're looking for a way to store the user input into a variable, here is how you can do :
Using a command
You have defined rr2 as your prefix. Let's assume you are using a cog system :
#commands.command()
async def info(self, ctx, *, input: str):
await ctx.send(input)
return
The await ctx.send(input) line will send a message into the channel where the command has been used that contains what the user had passed as input.
So we have :
>>> rr2 info This is my input.
Output :
'This is my input.'
Using on_message event
If you absolutely want to use an event to store the user input you can use :
#client.event
async def on_message(message):
if message.content.startswith('rr2.info.'):
input = message.content # this will send the message with the 'rr2.info.' prefix
channel = message.channel
await channel.send(input)
This will have the exact same behaviour as the command() solution.
#client.event
async def on_message(message):
if message.content.startswith('rr2.info.'):
# Split method returns a list of strings after breaking the given string by the specified separator
# Example input is 'rr2.info.uwu' then after calling input.split('rr2.info.')
# we will get a list of ['', 'uwu'] , that's why we need to call second index to get the string after
# 'rr2.info.' with [1]
suffix = message.content.split('rr2.info.')[1]
# Don't send it if it's empty, it will be empty if the user for example sent just 'rr2.info.'
if suffix:
await message.channel.send(suffix)
else:
# Overriding the default provided on_message forbids any extra commands from running.
# To fix this, we add client.process_commands(message) line at the end of our on_message
await client.process_commands(message)
I also suggest changing the name client to bot it's nicer.
If you would like to do this same thing with commands you would have to change the source code of commands ext functions, mainly because the space is used as separator between commands/arguments and you want to pass that as a single string.

How do I send the text in a file discord.py

I have made a .txt file in my work file and it doesn't work when I write
this
#client.command()
async def readlist():
await client.say('here you go')
await client.send_file('blacklist.txt')
send_file requires that a destination be passed as the first parameter (such as a text channel object.)
You would need get the channel object within readlist somehow, the easiest way probably being pass_context=True in the command annotation and adding a parameter to readlist for the context object passed. E.g.
#client.command(pass_context=True)
async def readlist(ctx):
# you can now get the channel the command was called in with ctx.message.channel

Resources