Use asyncio with neovim remote plugins - python-asyncio

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))

Related

Discord.PY Custom Commands - can you define multiple commands/functions in one file?

Silly question, but in Discord PY are you able to define multiple custom commands or functions in one file? I am working on a few custom commands and just noticed that only the one defined first works, the others say command not found. They all work individually, just not when there are multiple commands defined. Is this format just wrong? Not getting any syntax errors and again it was just working until I added the Add/Remove Roles commands, or even just a Test command that does nothing..
Thanks!
My code looks something like this:
imports
define HelperFunctions:
APIInfo, etc
#start bot
bot = commands.Bot(command_prefix='$', intents=intents)
#bot.command()
#define bot commands
async def TestCommand(params):
do stuff
async def AddRoles(params):
do stuff
async def RemoveRoles(params):
do stuff
async def AddToList(params):
do stuff
bot.run()
bot.command() decorator should be used separately for every command.
https://github.com/Rapptz/discord.py/blob/master/examples/basic_bot.py
#define bot commands
#bot.command()
async def TestCommand(params):
do stuff
#bot.command()
async def AddRoles(params):
do stuff
#bot.command()
async def RemoveRoles(params):
do stuff
#bot.command()
async def AddToList(params):
do stuff

How do I make it so that ctx is not of type 'interaction' (nextcord slash commands)?

I'm trying to create a music bot using nextcord slash commands and interactions. The command isn't fully finished yet, as I am having trouble getting it to even join the voice channel. I don't exactly know how interactions work yet but I'm assuming it's a similar concept as ctx. Below is my music.py cog:
import nextcord
from nextcord.ext import commands
from nextcord import Interaction
class Music(commands.Cog):
def __init__(self, client):
self.client = client
guild_ids = ["Guild Ids Go Here"]
#slash commands go under here
#nextcord.slash_command(name="play", description="plays music in vc", guild_ids = guild_ids)
async def play(self, interaction : Interaction, query: str):
channel = interaction.author.voice.channel #ERROR IS HERE
try:
await channel.connect()
await interaction.response.send_message("The bot has joined vc.")
except:
await interaction.response.send_message("Failed to find voice channel.")
def setup(client):
client.add_cog(Music(client))
I'm getting an error that says "'Interaction' object has no attribute 'author'. It occurs on line 15 in 'play' when it says 'channel = interaction.author.voice.channel'. I think this means that this isn't the right way to go about getting the author's voice channel. If this is the case, is there a better, working method?
In nextcord interaction, message author is interaction.user,
channel is interaction.channel.
You also can send interaction message by interaction.send instead
of interaction.response.send_message. It's much shorter and easier
to read.
If you want to send a normal message without using interaction, try
interaction.channel.send. It's similar to ctx.channel.send in application command

I am trying to make my bot be able to respond to a command that starts with a certain set of characters instead of having an exact command

I am trying to make my bot be able to respond to a command that starts with a certain set of characters instead of having an exact command. I figured out how to use it with client.event but I can't figure out how to do it using client.command.
As an example let's say I want it to send a message that says "Hello!" when the command ".hi" is sent. I want it to work even if a user sends ".hii" or maybe ".hi!". I can get this to work by using the startswith command. Below is my working client.event code and my broken client.command code:
client.event code:
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.content.lower().startswith('.hi'):
await message.channel.send('Hello!')
client.command code:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.', case_insensitive=True)
#client.command()
async def hi(message):
if message.content.startswith('hi'):
print('Hello!')
If you are using the command, you could specify the aliases of the command like this:
#client.command(aliases=['hii', 'hi!'])
async def hi(ctx):
print('Hello!')
https://discordpy.readthedocs.io/en/latest/ext/commands/api.html?highlight=aliases#discord.ext.commands.Command.aliases
Also your command is broken because you need to pass in ctx as a parameter for a command, not message.
Your #client.command() won't work because you never passed in ctx, by the way, to send a message you might want to use await ctx.send("Hello!").
The "full command" would look something like this, though its not really what you want as you would have to type .hi hi
#client.command()
async def hi(ctx, message):
if message.startswith('hi'):
await ctx.send('Hello!')
If you actually want to do that you should try using your #client.event (with on_message, basically the one you already have) as I dont think that you can do that unless you use aliases (Which is not a good option though):
#client.command(aliases=['hi!', 'hiiiiii', 'hello'])
async def hi(ctx):
await ctx.send('Hello!')
In conclusion, if you really want to do something like this, use your exsistent #client.event with on_message
(Just as #moinierer3000 said)

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/

On *any* event discord.py

I want to run a certain block of code for every event. I figured that it would probably be easiest if there was something like on_any_event. However, I can't seem to find anything in the docs or on the web. Does anyone know if there is a way to do this, and if so, how?
Other Info:
discord.py-rewrite
Thanks in advance.
I see two possibilities:
Most of the events in discord.py, afaik, is "handlers" for sockets responses.
You can try to use on_socket_response(message) event. This should be enough for all websocket-based events.
If you need literally any event, you can try overwrite dispatch function in child class, and use this class as your's bot's class.
In example:
from discord.ext import commands
class MyBot(commands.Bot):
def dispatch(self, event_name, *args, **kwargs):
super().dispatch("event", event_name)
super().dispatch(event_name, *args, **kwargs)
bot = MyBot(command_prefix="!")
This will dispatch additional event on any event
#bot.event
async def on_event(event_name):
print(f"{event_name} is dispatched")

Resources