So, I have created a command that executes a reoccurring message. I have it set to 10 seconds in the code for testing purposes, but I will have the message loop every 4 hours. I don't get any errors when running the code, but the command is not actually executing. The message never sends and I am confused as to why and can't seem to figure it out.
import discord
import discord.utils
import asyncio
import http.client
import aiohttp
import EZPaginator
import platform
import yaml
import os
from yaml import load, dump
from discord.ext import commands
from discord.ext import tasks
intents = discord.Intents.default()
client = commands.Bot(command_prefix = '$', intents=intents)
client.remove_command('help')
#client.event
async def on_ready():
print('ZOF BOT IS ONLINE!')
# Reoccuring Message Reminder
#tasks.loop(seconds=10)
async def send_message(ctx):
channel = discord.utils.get(ctx.guild.channels, name='reminders')
remind = discord.Embed(title='**:rotating_light:ATTENTION ZOF:', description='Do not forget to donate to Fortress, Alliance tech, and Ruins!', color=0xFFFFFF)
print('Reoccurring Message Sent.')
await channel.send(embed=remind) ```
You need to start you task function for example in on_ready event:
#client.event
async def on_ready():
if not send_message.is_running():
send_message.start()
Related
I try to make a discord bot on python but he not responding for any command.
How i can fix it?
import requests
import random
from discord.ext import commands
intents = discord.Intents.all()
Away = commands.Bot(command_prefix="$", intents=intents)
#Away.event
async def on_message(ctx):
print(ctx.content)
#Away.command
async def say(ctx):
await ctx.send(ctx.content)
Away.run("token")```
Please anyone help to solve it. Bot is being run successfully without getting error but bot is not working / not sending messages.
It is not working.
This is the code:
try:
import requests,json,asyncio,pytz,time,discord
from dhooks import Webhook, Embed
import datetime
import asyncio
from pytz import timezone
from discord.ext.commands import Bot
from discord.ext import commands
import sys
import subprocess
except Exception as e:
print("Import Error:",str(e))
# input all datas
bot1_token = "bot1_token"
bot2_token = "bot2_token"
bot_prefix = "#"
Vedantu_channels = ["873257403217481738"]
client = discord.Client()
bot = Bot(command_prefix=bot_prefix)
bot.remove_command('help')
#bot.event
async def on_ready():
print(f"Logged in as {bot.user.name}")
ch = bot.get_channel(853480780214698014)
#client.event
async def on_ready():
print(f"Logged in as {client.user.name}")
#client.event
async def on_message(message):
if message.channel.id in Vedantu_channels:
content = message.content
if "w1" in content:
if "w1" in content or "w1" in content:
await ch.send("1")
if message.channel.id in Vedantu_channels:
content = message.content
if "w2" in content:
if "w2" in content or "w2" in content:
await ch.send("2")
loop = asyncio.get_event_loop()
loop = asyncio.get_event_loop()
loop.create_task(bot.start(bot2_token))
loop.create_task(client.start(bot1,bot=True))
while True:
try:
try:
loop.run_forever()
finally:
loop.stop()
except Exception as e:
print("Event loop error:", e)
Can anyone help me to fix this issue?
I am not getting any error but messages not send bot to channel.
There is no error messages that I have gotten.
Well, your code is very complicated. You can simplify it a lot.
Your errors:
You must put all imports in multiple imports, and remove the try and except
You can't do variables with underscores and numbers
You can't use discord.Client() and commands.Bot()
It isn't recommended to use two bots in a same project
You can't use client and bot as the same time
You must use [] when you have more than one object
simplified code
from discord.ext import commands
from discord.ext.commands import bot
from pytz import timezone
from dhooks import Webhook, Embed
import discord
import json
import asyncio
import pytz
import requests
import subprocess
import datetime
import os
import time
TOKEN = "paste the bot's token here"
bot = commands.Bot(command_prefix="#")
bot.remove_command("help")
#bot.event
async def on_ready():
print("Logged in as {0.user}".format(bot))
#just an example command
#bot.command()
async def hello(ctx):
channel = bot.get_channel(853480780214698014)
await channel.send("Hello!")
await ctx.send("You can see my greetings in another channel")
bot.run("TOKEN")
Documentation: https://discordpy.readthedocs.io/en/stable/
How can I send a message every x time with a discord bot with discord.py?
Here is my code:
import discord
from discord.ext import commands
from discord.ext import tasks
import os
TOKEN = 'xxxxxxxxxxx'
bot = commands.Bot(command_prefix='!',help_command=None,activity=discord.Game(name="!help"))
#tasks.loop(seconds=5)
async def ping(channel):
print("is the function being called")
channel = bot.get_channel(877115461014282320)
await channel.send('embed=embed')
bot.run(TOKEN)
Edit: I know I shouldn't leak my key but I edited it after 10 seconds and removed a letter why could someone still access it?
You have to start task using ping.start() in event on_ready or in some command.
import discord
from discord.ext import commands
from discord.ext import tasks
import os
TOKEN = os.getenv('DISCORD_TOKEN')
bot = commands.Bot(command_prefix='!', help_command=None, activity=discord.Game(name="!help"))
#tasks.loop(seconds=5)
async def ping():
print("is the function being called")
#channel = bot.get_channel(877115461014282320)
#await channel.send('embed=embed')
#bot.event
async def on_ready():
print('on ready')
ping.start()
bot.run(TOKEN)
You have also ping.stop(), ping.cancel(), etc. See doc: tasks
Basically whenever I run this code it gives me an error, I am trying to make the code server mute one of my friends every 20 seconds for 5 seconds, it says there is something wrong with the line where the user is being defined.
import discord
from discord.ext import commands
from discord.ext import commands, tasks
import random
import datetime
from datetime import date
import calendar
import time
import asyncio
from discord.ext.tasks import loop
client = commands.Bot(command_prefix='.')
#tasks.loop(seconds=20)
async def mute_person():
user = await discord.Guild.fetch_member("670549896247509002", "670549896247509002") # Get the Member Object
await user.edit(mute=True) # Mute
await asyncio.sleep(20) # Waits for 20 seconds then unmute.
await user.edit(mute=False) # Unmute
#client.event
async def on_ready():
print("I am ready")
mute_person.start()
client.run("Token")
You are not using the right function discord.Guild doesn't exist as you are using it. the main use must be await guild.fetch_member(member_id) and then guild means the guild object which has a specific ID as well. So you need to get the guild first.
I am not able to change the status. I want to develop my own bot and have to add more features too
# Importing Modules
import discord
import asyncio
import random
import DateTime
from discord.ext import commands
from urllib import parse, request
import re
import time
# Importants
bot = commands.Bot(command_prefix='*', description="This is a Helper Bot")
client = discord.Client()
# ***ADDING FEATURES***
# Getting Online And Changing status
async def my_background_task():
await client.wait_until_ready()
# Setting `Playing ` status
await bot.change_presence(activity=discord.Game(name="With Your Life"))
time.sleep(5)
# Setting `Streaming ` status
await bot.change_presence(activity=discord.Streaming(name="Kartik Jain", url="https://youtube.com/c/KartikJainChannel"))
time.sleep(5)
# Setting `Listening ` status
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="To *help"))
time.sleep(5)
# Setting `Watching ` status
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=" Your Nonsense"))
time.sleep(5)
# While Loop To loop changing statuses
while not bot.is_closed():
bot.bg_task = bot.loop.create_task(my_background_task())
# Making the bot online
client.run('My_ID')
You have to change the bot with client and you have to change this my_background_task() to this: on_ready().
Just look at this code:
import discord
import asyncio
import random
import DateTime
from discord.ext import commands
from urllib import parse, request
import re
import time
# Importants
bot = commands.Bot(command_prefix='*', description="This is a Helper Bot")
client = discord.Client()
# ***ADDING FEATURES***
# Getting Online And Changing status
#client.event
async def on_ready():
await client.change_presence(activity=discord.Streaming(name="My live",url="http://www.twitch.tv/G4merStop"))
time.sleep(5)
await client.change_presence(activity=discord.Streaming(name="Your live",url="http://www.twitch.tv/G4merStop"))
time.sleep(5)
# ^^^^ here you can see what you can do in the docs ^^^^
# Making the bot online
client.run('My_ID')```
First of all, you're actually creating 2 different instances of the client, client using discord.Client and bot using commands.Bot. You're using bot to change the presence, and in the end, you're running the client instance, which makes part done using bot totally useless.
You don't need to use discord.Client, because commands.Bot has all the methods and attributes of discord.Client.
Next, you should change time.sleep() to await asyncio.sleep() because it's blocking your code and also put the bot.loop.create_task() method inside on_ready event