I'm using Interactions.py (client = interactions.Client) so that I can use its sophisticated slash commands system, but as a result the on_message event method is no longer triggered. When I use Discord.py (client = discord.Client) the on_message method works successfully.
How do I get on_message to work while using the slash command system of Interactions.py?
import os
import os.path
import interactions
import mysql.connector
import ast
from asyncio.windows_events import NULL
import operator as op
import discord
import inspect
from math import sqrt
from dotenv import load_dotenv
intents = discord.Intents
intents.messages = True
client = interactions.Client(token=TOKEN)
#client.command(
#command details here
)
async def count(ctx: interactions.CommandContext, command: str):
#manage incoming commands
#this only works correctly when I use client = interactions.Client
#client.event
async def on_message(message):
#do things based on message contents
#this only works correctly when I use client = discord.Client
client.Start()
Thanks!
It would be on_message_create, as this is the name that the discord api uses
Related
I am writing a discord bot which triggers a web scraping function every 5 minutes. Even though I am running the function by calling client.loop.create_task, it still results in the following error every time:
Shard ID None heartbeat blocked for more than 10 seconds.
What can be changed to fix this error? I thought I had changed my code to call the function in a non-blocking way but it seems it still blocks.
import discord
import os
from dotenv import load_dotenv
from scrape import *
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
load_dotenv()
intents = discord.Intents.default()
intents.messages = True
intents.message_content = True
# instantiate discord client
client = discord.Client(intents=intents)
async def trigger_scrape():
client.loop.create_task(scrape(client))
# discord event to check when the bot is online
#client.event
async def on_ready():
scheduler = AsyncIOScheduler(standalone=True)
scheduler.add_job(trigger_scrape, CronTrigger(minute="*/5")) # run every 5 minutes
scheduler.start()
client.run(os.getenv('TOKEN'))
How can I get the channel name with its channel_id using the pyrogram library?
import time
from pyrogram import Client
from pyrogram import types, filters
from loguru import logger
import config
app = Client(
"asyncBot",
api_id=config.api_id,
api_hash=config.api_hash
)
clicks = 0
#app.on_message()
async def my_handler(client: Client, message: types.Message):
cchat = client.get_chat.title(config.CHANNEL_ID)
print(cchat)
app.run()
But this method doesn't work, I don't quite understand how to work with the title method in pyrogram, please advise!
enter image description here
Code:
import discord
import os
import sys
import asyncio
import time
import json
from discord.ext import commands
from discord.utils import get
bot = commands.Bot(command_prefix='*', intents=discord.Intents.all())
TOKEN= 'ODU1MDAwMzU3Njk2NDM4MzEy.Gxtt2S.IxUbC9bS8Ps3hTeUYpwxW_U7A6q0MuzTwJxUFA'
#bot.event
async def on_ready():
print(" Bot account ")
print()
print(f"Username: {bot.user.name}")
print(f"Bot ID: {bot.user.id}")
print(f"Bot token: {TOKEN}")
print()
bot.run(TOKEN)
Your code is throwing an discord.errors.PrivilegedIntentsRequired Exception as you can see in the bottom line of your screenshot.
This means it is trying to use intents that it has no permission to use. Either enable those intents in the discord developer portal or replace discord.Intents.all() with the intents that you really need and have sufficient permissions for.
I've an issue I can't fix.
Indeed, I'm trying to develop a discord bot, and when asking some questions by DM, the bot is publishing a message to a specific channel with a button, until there everything works fine.
But I'd try to catch the event on button click, I'm using discord-ui v5 with discord.py v1.7 and Python 3.8.
I register my listener which is well called, this listener aims to DM the user who clicked on.
Unfortunately, the listener is called twice, the direct issue is user has twice the same message, that I wouldn't have.
Here my code :
main.py :
import discord
import locale
import os
from discord.ext import commands
from discord_ui import UI, Button, Components, Listener
from dotenv import load_dotenv
from pathlib import Path
from src.workflows.main import MainWorkflow
from src.workflows.subscription import SubscriptionWorkflow
from src.listeners.awesome_listener import MyAwesomeListener
path = Path()
load_dotenv(path.resolve() / ".env")
intents = discord.Intents.default()
intents.members = True
locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
bot = commands.Bot(command_prefix = "!", intents=intents)
ui = UI(bot)
components = Components(bot)
workflow = MainWorkflow()
#bot.command()
async def my_command(ctx):
...
signup_channel = bot.get_channel(signup_channel_id)
""" EVERYTHING WORKS FINE UNTIL THERE """
sent_message = await signup_channel.send(message_for_run, components=[
Button(label="JLabel", color="green", custom_id="my_id"),
], listener=MyAwesomeListener(workflow))
bot.run(os.getenv("DISCORD_BOT_TOKEN"))
awesome_listener.py :
from discord_ui import Listener
from src.workflows.subscription import SubscriptionWorkflow
class NewRunListener(Listener):
def __init__(self, workflow) -> None:
self.subscription_workflow = SubscriptionWorkflow()
self.workflow = workflow
#Listener.button("my_id")
async def my_func(self, ctx):
""" THE CODE BELOW IS EXECUTED TWICE """
cheers = await self.subscription_workflow.cheers(ctx, self.workflow.run)
await ctx.author.send(cheers)
I'm pretty sure it's an idiot mistake but I can't figure it out.
Any help would be appreciated !
Thanks !
From what I understand you want to know who clicked a button. In that case you can use: interaction = await self.bot.wait_for("button_click") and then it’s simple to grab the user using interaction.user
im trying to Import commands from another PY file. But it always Says command not exists...
This is my code in the file named test.py
from discord import Embed
from random import choice
from colorama import *
from os import path
from plyer import notification
intents = discord.Intents().all()
client = commands.Bot(command_prefix = ">", intents=intents)
#commands.command()
async def haha(ctx):
await ctx.send("hi")
import customcmd
client.run("censored")
The code in customcmd.py is
import discord
from discord.ext import commands, tasks
#commands.command()
async def hi(ctx):
await ctx.send(hi)#
Try this
from .customcmd import *