how to insert #client.event in #client.command just new to discord py program i hava a question about this followingcode - discord

is it possiple to insert client.event on client.command
this is my code please explain this issue and add your code for future reference
import discord
from discord.ext import commands
import random
import asyncio
from random import randint
import aiohttp
from discord.ext.commands.core import command
client = commands.Bot(command_prefix = '')
client.remove_command('help')
#client.event
async def on_ready():
print('we are logged in {0.user}'.format(client))
#client.command()
async def say(ctx, *, args):
embed=discord.Embed(title= args, color=0x3700ff)
await ctx.send(embed=embed)
#client.event
async def on_message(message):
if message.content.startswith('stop'):
await message.channel.send('start')
#client.run("my token")
am try to understand why #client.event on_message will not working on #client.command
disclimer:- am noob on code so need a professionals help so thank about the helper

Related

will that work? its a bot that responds to messages from a certain user

import discord
from replit import db
from keep_alive import keep_alive
intents = discord.Intents.default()
client = discord.Client(intents=intents)
#client.event
async def on_message(message):
if message.author.id == "667020238340096000":
await message.channel.send("ok naplet")
keep_alive()
client.run(
"token goes here but i deleted it to share this")
will it work? help me

how can I add context menu from app_commands

I want to add context menu from app_commands, I tried that, but it didn't work
import discord
from discord import app_commands
from discord.ext import commands
#app_commands.context_menu(name="Hello")
async def hello(interaction: discord.Interaction, message: discord.Message):
await interaction.response.send_message("Hey !")
I'm using discord.py 2.0.1, how can I add?
Context menu's are a bit tricky to get working, here's an example:
async def hello(interaction: discord.Interaction, message: discord.Message):
await interaction.response.send_message(f"Hey {message.author.mention}!")
hello_context_menu = app_commands.ContextMenu(
name='Say Hello',
callback=hello,
)
bot.tree.add_command(hello_context_menu)

AttributeError: 'Client' object has no attribute 'get_all_emojis'

from distutils.util import change_root
from discord.utils import get
import discord
import random
import shutil
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
channel = client.get_channel(968268255804420096)
emoji = await discord.utils.get(client.get_all_emojis(), name='upvote')
await client.add_reaction(message, emoji)
i'm trying to make a bot react to all messages in a specific channel, but i keep getting the "AttributeError: 'Client' object has no attribute 'get_all_emojis'" error
any help?
This no longer works because get_all_emojis was removed a long time ago. Instead, use client.emojis.
emoji = discord.utils.get(client.emojis, name='upvote')

Continuous Data Transfer with Flask Socket Io and Reactjs

I'm trying to stream data every second from my server to the frontend. Currently I could only connect to the socket io and all the tutorials I found was using a chat application as an example.
The project consist of pymodbus so I could communicate to a plc and gather those data. The data is then visualize using reactjs
Python
from flask import Flask, jsonify
from flask_restful import Api, Resource
from flask_socketio import SocketIO, emit
import random
app = Flask(__name__)
app.config["SECRET_KEY"] = "somethingRandom"
app.config["DEBUG"] = True
api = Api(app)
socketio = SocketIO(app, cors_allowed_origins="*")
#app.route("/")
def hello_world():
return jsonify(mssg="hello world")
def PlcTankData():
i = 5
j = 0
while j != 5: # should be while True
j+=1
sample = random.randint(1, 10)
print(sample)
emit("newdata", {"td": sample}, namespace="/home")
socketio.sleep(1)
#socketio.on("connect", namespace="/home")
def frontend_connection():
print("Client is Connected")
emit("connect", {"hello": "world"})
PlcTankData()
class MachineStatus(Resource):
def put(self):
data = request.json
print(data)
api.add_resource(MachineStatus, '/machine-stat')
if __name__ == '__main__':
socketio.run(app, host="192.168.0.105", port=5000)
Reactjs
import React, { useState, useEffect } from 'react'
import axios from 'axios'
import io from 'socket.io-client'
//To Control the machcine
const handleMachineStatus = async (e) => {
await axios
.put('http://192.168.0.105:5000/machine-status', { status: e.target.name })
.then((res) => {
console.log(res)
})
.catch((err) => console.log(err))
}
//To get real time data
useEffect(() => {
const socket = io.connect('http://192.168.0.105:5000/home')
socket.on('connect', (data) => console.log(data))
socket.on('newdata', (data) => console.log(data))
}, [])
I know that the while loop is the one causing the trouble cause the data is being thrown after the while loop is finish.
PS: I'm not sure if the react code also has a problem so I'll tag it.
The connect handler is not a good place to run a lengthy task, because Flask-SocketIO completes the connection process once you return from this handler.
I suggest you move your PlcTankData function to a separate event instead of calling it from the connect handler.

Discord.py not unpinning own bot's messages

My test code is
#bot.command(name='test')
async def test(ctx):
message = await ctx.send('test')
channel = ctx.channel
message = await channel.fetch_message(message.id)
await message.pin()
await message.unpin()
The bot pins the message but it won't unpin it.
I tested the code, it works fine for me.
NOTE- Discord only shows a message that someone has pinned the message, but discord does not show if someone has unpinned it, you need to check it in the pinned messages.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix = '!')
#bot.event
async def on_ready():
print('Logged successfully!')
#bot.command()
async def test(ctx):
message = await ctx.send('test')
await message.pin()
await message.unpin()
bot.run('BOT_TOKEN_HERE')

Resources