For some reason my bot always turns off without printing any output to the command line or showing any kind of error. The bot functions properly for a few hours after being turned on. Basic code looks like this:
app = Client("my_account", '123456', '123456789abcd')
TESTING = "321"
USER_ID = "123"
chat_mapping = {TESTING: "-10011111111111", USER_ID: "-10011111111111"}
#app.on_message()
def my_handler(client, message):
if str(message.chat.id) not in chat_mapping:
return
elif str(message.chat.id) == USER_ID:
storeMsg(message)
else:
print(message.text)
app.run()
Any advice would be greatly appreciated!
if str(message.chat.id) not in chat_mapping
in this lane, your statement will check if message.chat.id is equal one of the keys of dictionary, not values.
Means your message.chat.id can't be 123 or 321.
USER_ID = some id
chat_mapping = [some ids]
#app.on_message()
def my_handler(client, message):
if str(message.chat.id) not in chat_mapping:
return
elif str(message.chat.id) == USER_ID:
storeMsg(message)
else:
print(message.text)
app.run()
Related
I'm returning a string but it says it's not working.
I'm somewhat new to discord bot coding and am confused.
Code
from python_aternos import Client
aternos = Client.from_credentials('test', '123')
servs = aternos.list_servers()
testserv = None
for serv in servs:
if serv.address == 'server.aternos.me':
testserv = serv
def handle_response(message) -> str:
p_message = message.lower()
if p_message == 'status':
return str(serv.status)
# Here the issue starts
if p_message == 'Start':
serv.start(False,True)
return "test"
# Here the issue ends
if p_message == 'help':
return "Status - Tells the status of the server.\nStart - Starts the server for people to play.\nHelp - Lists all the commands."
I tried to return a string and then use the start command but it doesn't work.
This is the first time t work with flask and pymongo. Anyone can tell my why and how to fix this problem ?
I had watched this video: https://www.youtube.com/watch?v=7Abxa0q4Vuk and use his code. However, it isn't work when i try to login.
It is a picture I captured when I tried to log in with an account had already register
This is the login_check code:
if(request.method == 'POST'):
req = request.form
req = dict(req)
print(req)
query = user_table.find({'uid', req['uid']})
flag = 0
temp = None
for x in query:
if(x['uid'] == req['uid']):
flag = 1
temp = x
break
if(flag == 1):
if(temp['password'] == req['password']):
return render_template('dashboard.html', uid = req['uid'])
else:
return render_template('invalid.html', message = 'incorrect password')
else:
return render_template('invalid.html', message = "User not registered")
return render_template('login.html')
This is the error:
filter must be an instance of dict, bson.son.SON, or any other type that inherits from collections.Mapping
#Bot.command()
async def kod(ctx,discordid):
objects = Code.manager(db)
code_list = list(objects.all())
i = 1
while i <= len(code_list):
usercode = objects.get(i)
usercode = usercode.__dict__
id = usercode.get('discord_id')
print(discordid)
print(id)
if discordid == id:
await ctx.send("worked")
i = i + 1
I have problem with if command.you can see the result of the print(id) and print(discordid) code here:
806114153915875378
806114153915875378
You can see that discordid and id are the same.
But
if discordid == id:
await ctx.send("worked")
Code doesnt work correctly and bot doesnt send message.There isnt a error printed at terminal.
Try this program
if str(discordid) == str(id):
await ctx.send("worked")
I am trying to make a function whenever the user reacts with the check emoji or if they react with the x. So basically I have a check if the user has a phone, if the user has a phone then you can call them. The bot sends a message in dms and reacts with a check and a x, if you click the check I want a function to happen, how can I do this?
#commands.command()
#commands.cooldown(1,120,commands.BucketType.user)
async def call(self, ctx, member : discord.Member):
phone = False
users = await bank_data()
user = ctx.author
client = discord.Client
try:
bag=users[str(user.id)]["bag"]
except:
bag=[]
for item in bag:
name = item["item"]
if name == "phone":
phone = True
if phone == False:
await ctx.reply("You must buy a phone to call someone idiot.")
if phone == True:
try:
targetBag = users[str(target.id)]["bag"]
except:
targetBag=[]
targetPhone = False
if target:
for item in targetBag:
name = item["item"]
if name =="phone":
targetPhone = True
if targetPhone == False:
await ctx.reply("Attempted user to call does not have a phone.")
if targetPhone == True:
channel = await target.create_dm()
emojis=['✅', '❌']
confirmEmoji = '✅'
message=await channel.send(f"{ctx.author} is calling you. Do you want to answer?")
for emoji in emojis:
await message.add_reaction(emoji)
You can use the wait_for() for this case. Create a response variable so that you can check and use the response to answer the phone or decline it.
You can read more about it here-
https://discordpy.readthedocs.io/en/stable/api.html#discord.Client.wait_for
There are different types of events which you can pass into wait_for() in your case it's reaction_add
Hope this helped :D
I have a question about building a telegram bot with python.
How can I get input from the user in my python-telegram-bot?
For example, a dictionary bot, how can I get a word from the user?
I recommend to have a look at the examples on GitHub.
With python-telegram-bot v12.0.0b1 you can do it like this:
def do_something(user_input):
answer = "You have wrote me " + user_input
return answer
def reply(update, context):
user_input = update.message.text
update.message.reply_text(do_something(user_input))
def main():
updater = Updater("TOKEN", use_context=True)
dp = updater.dispatcher
dp.add_handler(MessageHandler(Filters.text, reply))
updater.start_polling()
updater.idle()
I recommend pyTelegramBotAPI, not python-telegram-bot.
For me it is easier.
And you should use this:
#bot.message_handler(func=lambda message: True)
def echo_message(message):
cid = message.chat.id
mid = message.message_id
message_text = message.text
user_id = message.from_user.id
user_name = message.from_user.first_name
You can save this information in JSON if you want.